package com.atguigu.edu.controller;


import com.atguigu.common.util.R;
import com.atguigu.edu.entity.EduChapter;
import com.atguigu.edu.entity.EduCourse;
import com.atguigu.edu.entity.EduVideo;
import com.atguigu.edu.entity.vo.form.LessonFormVo;
import com.atguigu.edu.service.EduChapterService;
import com.atguigu.edu.service.EduCourseService;
import com.atguigu.edu.service.EduVideoService;
import com.atguigu.edu.service.client.VodClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author alexzshl
 * @since 2021-05-04
 */
@RestController
@RequestMapping("/api/edu/video")
@CrossOrigin
@Slf4j
public class EduVideoController {

    @Resource
    private EduCourseService eduCourseService;

    @Resource
    private EduChapterService eduChapterService;

    @Resource
    private EduVideoService eduVideoService;

    @Resource
    private VodClient vodClient;

    // 不能引入其他service包
    // @Resource
    // private VodService vodService;

    @GetMapping("/getVideo/{videoId}")
    public R getVideo(@PathVariable("videoId") String videoId) {
        EduVideo video = eduVideoService.getById(videoId);
        return R.ok().data("data", video);
    }

    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo video) {
        eduVideoService.addLesson(video);
        return R.ok();
    }

    /**
     * 更新 课时
     * 同时需要释放之前的云端视频资源
     * @param video
     * @return
     */
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo video) {
        // TODO: 更新前还需要检查并删除之前阿里云上存储的视频(是否可以在前端完成？似乎是一个逻辑问题)
        // String oldVideoSourceId = eduVideoService.getById(video.getId()).getVideoSourceId();
        // ERROR
        // vodService.removeVideo(oldVideoSourceId);

        // 更新 课时 数据
        eduVideoService.updateLesson(video);
        return R.ok();
    }

    /**
     * 删除 课时、小节
     * 同时需要删除绑定的远端视频资源
     * @param videoId
     * @return
     */
    @PostMapping("/deleteVideo/{videoId}")
    public R deleteVideo(@PathVariable("videoId") String videoId) {
        EduVideo lesson = eduVideoService.getById(videoId);
        String videoSourceId = lesson.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            vodClient.deleteVideo(videoSourceId);
            log.info("删除视频");
        }
        eduVideoService.deleteLesson(videoId);
        return R.ok();
    }

    @GetMapping("/getVideoForm/{videoId}")
    public R getVideoForm(@PathVariable("videoId") String videoId) {
        EduVideo eduVideo = eduVideoService.getById(videoId);
        EduChapter eduChapter = eduChapterService.getById(eduVideo.getChapterId());
        EduCourse eduCourse = eduCourseService.getById(eduChapter.getCourseId());

        LessonFormVo lessonFormVo = new LessonFormVo();
        BeanUtils.copyProperties(eduVideo, lessonFormVo);
        lessonFormVo.setChapterTitle(eduChapter.getTitle()).setCourseTitle(eduCourse.getTitle());

        return R.ok().data("data", lessonFormVo).data("videoInfo", lessonFormVo);
    }
}

